package cn.iocoder.yudao.module.social.service.content;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.social.controller.admin.content.vo.ContentCreateReqVO;
import cn.iocoder.yudao.module.social.controller.admin.content.vo.ContentPageReqVO;
import cn.iocoder.yudao.module.social.controller.admin.content.vo.ContentUpdateReqVO;
import cn.iocoder.yudao.module.social.convert.content.ContentConvert;
import cn.iocoder.yudao.module.social.dal.dataobject.content.ContentDO;
import cn.iocoder.yudao.module.social.dal.mysql.content.ContentMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import jakarta.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.social.enums.ErrorCodeConstants.CONTENT_NOT_EXISTS;

/**
 * 内容 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class ContentServiceImpl implements ContentService {

    @Resource
    private ContentMapper contentMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createContent(ContentCreateReqVO createReqVO) {
        // 插入
        ContentDO content = ContentConvert.INSTANCE.convert(createReqVO);
        contentMapper.insert(content);
        // 返回
        return content.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateContent(ContentUpdateReqVO updateReqVO) {
        // 校验存在
        validateContentExists(updateReqVO.getId());
        // 更新
        ContentDO updateObj = ContentConvert.INSTANCE.convert(updateReqVO);
        contentMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteContent(Long id) {
        // 校验存在
        validateContentExists(id);
        // 删除
        contentMapper.deleteById(id);
    }

    private void validateContentExists(Long id) {
        if (contentMapper.selectById(id) == null) {
            throw exception(CONTENT_NOT_EXISTS);
        }
    }

    @Override
    public ContentDO getContent(Long id) {
        return contentMapper.selectById(id);
    }

    @Override
    public List<ContentDO> getContentList(Collection<Long> ids) {
        return contentMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ContentDO> getContentPage(ContentPageReqVO pageReqVO) {
        return contentMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ContentDO> getContentListByAuthor(Long authorId) {
        return contentMapper.selectListByAuthorId(authorId);
    }

    @Override
    public List<ContentDO> getContentListByContainer(Long containerId) {
        return contentMapper.selectListByContainerId(containerId);
    }

    @Override
    public void validateContentList(Collection<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return;
        }

        List<ContentDO> contents = contentMapper.selectBatchIds(ids);
        if (contents.size() != ids.size()) {
            throw exception(CONTENT_NOT_EXISTS);
        }
    }

    @Override
    public List<ContentDO> getContentListByAuthorAndPlanet(Long authorId, Long planetId) {
        return contentMapper.selectListByAuthorIdAndPlanetId(authorId, planetId);
    }

    @Override
    public List<ContentDO> getContentListByContainerAndPlanet(Long containerId, Long planetId) {
        return contentMapper.selectListByContainerIdAndPlanetId(containerId, planetId);
    }

    @Override
    public PageResult<ContentDO> getContentPageByPlanet(ContentPageReqVO pageReqVO, Long planetId) {
        return contentMapper.selectPageByPlanetId(pageReqVO, planetId);
    }

}
